<template>
  <div class="confirmOrder" v-loading="loading">
    <!-- 头部 -->
    <div class="confirmOrder-header">
      <div class="header-content">
        <p>
          <i class="el-icon-s-order"></i>
        </p>
        <p>确认订单</p>
        <router-link to></router-link>
      </div>
    </div>
    <!-- 头部END -->

    <!-- 主要内容容器 -->
    <div class="content">
      <!-- 选择地址 -->
      <div class="section-address">
        <p class="title">收货地址</p>
        <font color="red" size="2">提示:双击可以删除地址!</font>
        <br />
        <br />
        <div class="address-body">
          <ul>
            <li
              v-for="(item, index) in address"
              :class="confirmAddress == item.id ? 'in-section' : ''"
              :key="item.id"
              @click="confirmAddress = item.id"
              @dblclick="removeItem(index)"
            >
              <h2>{{ item.linkman }}</h2>
              <p class="phone">{{ item.phone }}</p>
              <p class="address">
                <font color="green">{{ item.address }}</font>
              </p>
            </li>

            <li class="add-address" @click="isAdd = true">
              <i class="el-icon-circle-plus-outline"></i>
              <p>添加新地址</p>
            </li>
          </ul>
        </div>
      </div>
      <!-- 选择地址END -->

      <!-- 商品及优惠券 -->
      <div class="section-goods">
        <p class="title">商品及优惠券</p>
        <div class="goods-list">
          <ul>
            <li v-for="item in checkGoods" :key="item.id">
              <img
                :src="
                  item.product_picture.includes('http:')
                    ? item.product_picture
                    : $target + item.product_picture
                "
              />
              <span class="pro-name">{{ item.product_name }}</span>
              <span class="pro-price"
                >{{ item.product_price }}元 x {{ item.product_num }}</span
              >
              <span class="pro-status"></span>
              <span class="pro-total"
                >{{
                  calculatePrice(item.product_price, item.product_num)
                }}元</span
              >
            </li>
          </ul>
        </div>
      </div>
      <!-- 商品及优惠券END -->

      <!-- 配送方式 -->
      <div class="section-shipment">
        <p class="title">配送方式</p>
        <p class="shipment">包邮</p>
      </div>
      <!-- 配送方式END -->

      <!-- 发票 -->
      <div class="section-invoice">
        <p class="title">发票</p>
        <p class="invoice">电子发票</p>
        <p class="invoice">个人</p>
        <p class="invoice">商品明细</p>
      </div>
      <!-- 发票END -->

      <!-- 结算列表 -->
      <div class="section-count">
        <div class="money-box">
          <ul>
            <li>
              <span class="title">商品件数：</span>
              <span class="value">{{ getCheckNum }}件</span>
            </li>
            <li>
              <span class="title">商品总价：</span>
              <span class="value">{{ getTotalPrice.toFixed(2) }}元</span>
            </li>
            <li>
              <span class="title">活动优惠：</span>
              <span class="value">-0元</span>
            </li>
            <li>
              <span class="title">优惠券抵扣：</span>
              <span class="value">-0元</span>
            </li>
            <li>
              <span class="title">运费：</span>
              <span class="value">0元</span>
            </li>
            <li class="total">
              <span class="title">应付总额：</span>
              <span class="value">
                <span class="total-price">{{ getTotalPrice.toFixed(2) }}</span
                >元
              </span>
            </li>
          </ul>
        </div>
      </div>
      <!-- 结算列表END -->

      <!-- 结算导航 -->
      <div class="section-bar">
        <div class="btn">
          <router-link to="/shoppingCart" class="btn-base btn-return"
            >返回购物车</router-link
          >
          <a
            href="javascript:void(0);"
            @click="addAlipayOrder"
            class="btn-base btn-primary"
            >结算</a
          >
        </div>
      </div>
      <!-- 结算导航END -->
    </div>
    <!-- 主要内容容器END -->

    <!-- 删除 popover框-->

    <el-dialog title="删除提示!" :visible.sync="dialogVisible" width="30%">
      <span>
        <font color="red">{{ dialogValue }}</font></span
      >
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="remove()">确 定</el-button>
      </span>
    </el-dialog>

    <!-- 弹出框 -->

    <el-dialog title="添加地址" width="400px" center :visible.sync="isAdd">
      <el-form :model="add" status-icon ref="ruleForm" class="demo-ruleForm">
        <el-form-item prop="linkman">
          <el-input
            prefix-icon="el-icon-user-solid"
            placeholder="请输入收件人姓名!"
            v-model="add.linkman"
            maxlength="20"
            show-word-limit
          ></el-input>
        </el-form-item>
        <el-form-item prop="phone">
          <el-input
            prefix-icon="el-icon-view"
            type="text"
            placeholder="请输入收件人电话"
            v-model="add.phone"
            maxlength="11"
            show-word-limit
          ></el-input>
        </el-form-item>
        <el-form-item prop="address">
          <el-input
            type="textarea"
            :autosize="{ minRows: 2, maxRows: 4 }"
            placeholder="请输入详细地址"
            v-model="add.address"
          >
          </el-input>
        </el-form-item>

        <el-form-item>
          <el-button
            size="medium"
            type="primary"
            @click="save()"
            style="width: 100%"
            >保存</el-button
          >
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>
<script>
// import { mapGetters } from "vuex";
import { mapActions } from "vuex";
export default {
  name: "",
  data() {
    return {
      // 虚拟数据
      confirmAddress: 0, // 选择的地址id,
      isAdd: false,
      dialogVisible: false, //dialog框显示
      dialogValue: null, //删除提示语
      delId: 0, //删除的id
      delIndex: 0,
      orderId: "", // 添加订单ID
      loading: false, // 添加loading状态
      //添加地址接口
      add: {
        linkman: "",
        phone: "",
        address: "",
      },

      // 地址列表
      address: [],
      checkGoods: [], // 结算商品信息
    };
  },
  // 组件内导航守卫
  beforeRouteEnter(to, from, next) {
    // 在渲染本组件的对应路由被确认前调用
    // 不能获取组件实例 `this`，因为守卫执行前，组件实例还没被创建
    // 通过 next 来访问组件实例
    next((vm) => {
      // 通过 `vm` 访问组件实例
      vm.getAddressList();
    });
  },
  // 从其他页面返回此页面时也会触发
  beforeRouteUpdate(to, from, next) {
    // 在当前路由改变，但是该组件被复用时调用
    // 可以访问组件实例 `this`
    this.getAddressList();
    next();
  },
  created() {
    // 清空旧数据
    this.checkGoods = [];

    // 获取路由参数中的订单ID
    this.orderId = this.$route.query.orderId;
    if (!this.orderId) {
      this.notifyError("订单ID不能为空");
      this.$router.push({ path: "/shoppingCart" });
      return;
    }

    // 获取地址列表
    this.getAddressList();

    // 获取订单详情
    this.getCheckGoods();
  },
  beforeDestroy() {
    // 组件销毁前清空数据
    this.checkGoods = [];
  },
  watch: {
    orderId: {
      handler() {
        this.getCheckGoods();
      },
      immediate: true, // 立即执行
    },
  },
  computed: {
    // 结算的商品数量; 结算商品总计; 结算商品信息
    getCheckNum() {
      return this.checkGoods.reduce(
        (total, item) => total + item.product_num,
        0
      );
    },
    getTotalPrice() {
      return this.checkGoods.reduce(
        (total, item) => total + item.product_price * item.product_num,
        0
      );
    },
  },
  methods: {
    ...mapActions([
      "deleteShoppingCart",
      "unshiftShoppingCart",
      "addShoppingCartNum",
    ]),
    getCheckGoods() {
      if (!this.orderId) return;

      this.$axios
        .post("/api/order/details", {
          order_id: this.orderId,
        })
        .then((res) => {
          if (res.data.code === "001") {
            console.log("订单详情数据:", res.data.data);
            this.checkGoods = res.data.data;
          } else {
            this.notifyError(res.data.msg || "获取订单详情失败");
          }
        })
        .catch((err) => {
          console.error("获取订单详情失败:", err);
          this.notifyError("获取订单详情失败，请重试");
        });
    },

    // 获取地址列表
    getAddressList() {
      // 从API获取地址列表
      this.$axios
        .post("/api/user/address/list", {
          user_id: this.$store.getters.getUser.user_id,
        })
        .then((res) => {
          if (res.data.code === "001") {
            this.address = res.data.data;
          }
        })
        .catch((err) => {
          console.error("获取地址列表失败:", err);
        });
    },
    // 计算价格，并且保留两位小数
    calculatePrice(price, num) {
      return (price * num).toFixed(2);
    },

    // 删除地址
    removeItem(index) {
      this.dialogVisible = true;
      this.dialogValue = "确定要删除该地址吗?";
      this.delIndex = index;
      this.delId = this.address[index].id;
    },

    //删除地址数据!
    remove() {
      this.$axios
        .post("/api/user/address/remove", {
          id: this.delId,
        })
        .then((res) => {
          switch (res.data.code) {
            // "001"代表结算成功
            case "001":
              this.delId = 0;
              this.dialogVisible = false;
              this.address.splice(this.delIndex, 1);
              this.delIndex = 0; //要删除的下角标
              // 提示结算结果
              this.notifySucceed(res.data.msg);
              break;
            default:
              // 提示失败信息
              this.notifyError(res.data.msg);
          }
        })
        .catch((err) => {
          return Promise.reject(err);
        });
    },

    save() {
      this.$axios
        .post("/api/user/address/save", {
          user_id: this.$store.getters.getUser.user_id,
          add: this.add,
        })
        .then((res) => {
          switch (res.data.code) {
            // "001"代表结算成功
            case "001":
              this.isAdd = false; //隐藏弹出框
              this.address = res.data.data; //接收新地址内容
              // 提示结算结果
              this.notifySucceed(res.data.msg);
              break;
            default:
              // 提示失败信息
              this.notifyError(res.data.msg);
          }
        })
        .catch((err) => {
          return Promise.reject(err);
        });
    },
    // 通过支付宝沙箱支付
    async addAlipayOrder() {
      try {
        if (!this.confirmAddress) {
          return this.notifyError("请选择收货地址");
        }

        // 添加加载状态
        this.loading = true; // 防止用户重复提交

        const res = await this.$axios.post("/api/order/pay", {
          order_id: this.orderId, // 使用传递过来的订单ID
          address_id: this.confirmAddress,
        });

        if (res.data.code === "001") {
          // 支付成功后删除购物车中的商品
          for (let i = 0; i < this.getCheckGoods.length; i++) {
            const temp = this.getCheckGoods[i];
            this.deleteShoppingCart(temp.id);
          }

          const div = document.createElement("div");
          div.innerHTML = res.data.data.payForm;
          document.body.appendChild(div);
          const form = div.querySelector("form");
          if (form) {
            form.submit(); // 提交表单完成支付流程
          } else {
            this.notifyError("支付表单生成失败");
          }
        } else {
          this.notifyError(res.data.msg || "支付失败");
        }
      } catch (err) {
        console.error("支付失败:", err);
        this.notifyError("支付系统异常，请稍后重试");
      } finally {
        this.loading = false;
      }
    },
  },
};
</script>
<style scoped>
.confirmOrder {
  background-color: #f5f5f5;
  padding-bottom: 20px;
}
/* 头部CSS */
.confirmOrder .confirmOrder-header {
  background-color: #fff;
  border-bottom: 2px solid #ff6700;
  margin-bottom: 20px;
}
.confirmOrder .confirmOrder-header .header-content {
  width: 1225px;
  margin: 0 auto;
  height: 80px;
}
.confirmOrder .confirmOrder-header .header-content p {
  float: left;
  font-size: 28px;
  line-height: 80px;
  color: #424242;
  margin-right: 20px;
}
.confirmOrder .confirmOrder-header .header-content p i {
  font-size: 45px;
  color: #ff6700;
  line-height: 80px;
}
/* 头部CSS END */

/* 主要内容容器CSS */
.confirmOrder .content {
  width: 1225px;
  margin: 0 auto;
  padding: 48px 0 0;
  background-color: #fff;
}

/* 选择地址CSS */
.confirmOrder .content .section-address {
  margin: 0 48px;
  overflow: hidden;
}
.confirmOrder .content .section-address .title {
  color: #333;
  font-size: 18px;
  line-height: 20px;
  margin-bottom: 20px;
}
.confirmOrder .content .address-body li {
  float: left;
  color: #333;
  width: 220px;
  height: 178px;
  border: 1px solid #e0e0e0;
  padding: 15px 24px 0;
  margin-right: 17px;
  margin-bottom: 24px;
}
.confirmOrder .content .address-body .in-section {
  border: 1px solid #ff6700;
}
.confirmOrder .content .address-body li h2 {
  font-size: 18px;
  font-weight: normal;
  line-height: 30px;
  margin-bottom: 10px;
}
.confirmOrder .content .address-body li p {
  font-size: 14px;
  color: #757575;
}
.confirmOrder .content .address-body li .address {
  padding: 10px 0;
  max-width: 180px;
  max-height: 88px;
  line-height: 22px;
  overflow: hidden;
}
.confirmOrder .content .address-body .add-address {
  text-align: center;
  line-height: 30px;
}
.confirmOrder .content .address-body .add-address i {
  font-size: 30px;
  padding-top: 50px;
  text-align: center;
}
/* 选择地址CSS END */

/* 商品及优惠券CSS */
.confirmOrder .content .section-goods {
  margin: 0 48px;
}
.confirmOrder .content .section-goods p.title {
  color: #333;
  font-size: 18px;
  line-height: 40px;
}
.confirmOrder .content .section-goods .goods-list {
  padding: 5px 0;
  border-top: 1px solid #e0e0e0;
  border-bottom: 1px solid #e0e0e0;
}
.confirmOrder .content .section-goods .goods-list li {
  padding: 10px 0;
  color: #424242;
  overflow: hidden;
}
.confirmOrder .content .section-goods .goods-list li img {
  float: left;
  width: 30px;
  height: 30px;
  margin-right: 10px;
}
.confirmOrder .content .section-goods .goods-list li .pro-name {
  float: left;
  width: 650px;
  line-height: 30px;
}
.confirmOrder .content .section-goods .goods-list li .pro-price {
  float: left;
  width: 150px;
  text-align: center;
  line-height: 30px;
}
.confirmOrder .content .section-goods .goods-list li .pro-status {
  float: left;
  width: 99px;
  height: 30px;
  text-align: center;
  line-height: 30px;
}
.confirmOrder .content .section-goods .goods-list li .pro-total {
  float: left;
  width: 190px;
  text-align: center;
  color: #ff6700;
  line-height: 30px;
}
/* 商品及优惠券CSS END */

/* 配送方式CSS */
.confirmOrder .content .section-shipment {
  margin: 0 48px;
  padding: 25px 0;
  border-bottom: 1px solid #e0e0e0;
  overflow: hidden;
}
.confirmOrder .content .section-shipment .title {
  float: left;
  width: 150px;
  color: #333;
  font-size: 18px;
  line-height: 38px;
}
.confirmOrder .content .section-shipment .shipment {
  float: left;
  line-height: 38px;
  font-size: 14px;
  color: #ff6700;
}
/* 配送方式CSS END */

/* 发票CSS */
.confirmOrder .content .section-invoice {
  margin: 0 48px;
  padding: 25px 0;
  border-bottom: 1px solid #e0e0e0;
  overflow: hidden;
}
.confirmOrder .content .section-invoice .title {
  float: left;
  width: 150px;
  color: #333;
  font-size: 18px;
  line-height: 38px;
}
.confirmOrder .content .section-invoice .invoice {
  float: left;
  line-height: 38px;
  font-size: 14px;
  margin-right: 20px;
  color: #ff6700;
}
/* 发票CSS END */

/* 结算列表CSS */
.confirmOrder .content .section-count {
  margin: 0 48px;
  padding: 20px 0;
  overflow: hidden;
}
.confirmOrder .content .section-count .money-box {
  float: right;
  text-align: right;
}
.confirmOrder .content .section-count .money-box .title {
  float: left;
  width: 126px;
  height: 30px;
  display: block;
  line-height: 30px;
  color: #757575;
}
.confirmOrder .content .section-count .money-box .value {
  float: left;
  min-width: 105px;
  height: 30px;
  display: block;
  line-height: 30px;
  color: #ff6700;
}
.confirmOrder .content .section-count .money-box .total .title {
  padding-top: 15px;
}
.confirmOrder .content .section-count .money-box .total .value {
  padding-top: 10px;
}
.confirmOrder .content .section-count .money-box .total-price {
  font-size: 30px;
}
/* 结算列表CSS END */

/* 结算导航CSS */
.confirmOrder .content .section-bar {
  padding: 20px 48px;
  border-top: 2px solid #f5f5f5;
  overflow: hidden;
}
.confirmOrder .content .section-bar .btn {
  float: right;
}
.confirmOrder .content .section-bar .btn .btn-base {
  float: left;
  margin-left: 30px;
  width: 158px;
  height: 38px;
  border: 1px solid #b0b0b0;
  font-size: 14px;
  line-height: 38px;
  text-align: center;
}
.confirmOrder .content .section-bar .btn .btn-return {
  color: rgba(0, 0, 0, 0.27);
  border-color: rgba(0, 0, 0, 0.27);
}
.confirmOrder .content .section-bar .btn .btn-primary {
  background: #ff6700;
  border-color: #ff6700;
  color: #fff;
}
/* 结算导航CSS */

/* 主要内容容器CSS END */
</style>